#include "StdAfx.h"
#include "CommandsRegistry.h"

CommandRegistry* CommandRegistry::instance_ = 0;

CommandRegistry& CommandRegistry::instance()
{
    if (!instance_)
    {
        instance_ = new CommandRegistry();
        atexit(&CommandRegistry::killInstance);
    }
    return *instance_;
}

void CommandRegistry::killInstance() 
{
    delete instance_;
    instance_ = 0;
}

void CommandRegistry::registerCommand(ICommand* ptr)
{
    commands_.push_back(ptr);
}

CommandRegistry::const_iterator  CommandRegistry::begin() const
{
    return commands_.begin();
}

CommandRegistry::const_iterator  CommandRegistry::end() const
{
    return commands_.end();
}

CommandRegistry::size_type CommandRegistry::size() const
{
    return commands_.size();
}

CommandRegistry::value_type CommandRegistry::operator[](size_type i) const
{
    const_iterator it = commands_.begin();
    while (i--) ++it;
    return *it;
}

CommandRegistry::~CommandRegistry()
{
    for (CommandsList::iterator i = commands_.begin(), end = commands_.end(); i != end; ++i)
    {
        delete (*i);
    }
}
